package com.cq.hd.rabbit.consumer;

import com.alibaba.fastjson.JSON;
import com.cq.hd.common.constant.RabbitMqConstant;
import com.cq.hd.common.entity.mq.NotifyMsgSendDto;
import com.cq.hd.system.api.NotifyMsgApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
@RabbitListener(queues = RabbitMqConstant.NOTIFY_MSG_QUEUE)
public class NotifyMsgConsumer {

    @Autowired
    private NotifyMsgApi notifyMsgApi;

    @RabbitHandler
    public void receiveMsg(Map map) {
        log.info("notifyMsg消息：{}", map.toString());
        NotifyMsgSendDto notifyMsgSendDto = JSON.parseObject((String) map.get("messageData"), NotifyMsgSendDto.class);

        try {
            notifyMsgApi.saveNotifyMsg(notifyMsgSendDto);
        } catch (Exception e) {
            log.error("消费notifyMsg消息失败，msg：{}", map.get("messageData"), e);
        }
    }

}
